package com.yanqu.road.server.manager.systemgroup.newmonopolymkt;

import com.yanqu.road.dao.impl.nenmonopolymarket.office.NewMonopolyMarketOfficeFinishDataDaoImpl;
import com.yanqu.road.dao.impl.nenmonopolymarket.office.NewMonopolyMarketOfficeSeatDataDaoImpl;
import com.yanqu.road.entity.enums.eMonopolyMarketResType;
import com.yanqu.road.entity.monopolymarket.office.MonopolyMarketOfficeSeatConfig;
import com.yanqu.road.entity.player.UserBaseInfo;
import com.yanqu.road.entity.systemgroup.newmonopolymarket.NewMonopolyMarketNoticeData;
import com.yanqu.road.entity.systemgroup.newmonopolymarket.config.NewMonopolyMarketResConfigData;
import com.yanqu.road.entity.systemgroup.newmonopolymarket.office.NewMonopolyMarketOfficeFinishData;
import com.yanqu.road.entity.systemgroup.newmonopolymarket.office.NewMonopolyMarketOfficeSeatData;
import com.yanqu.road.entity.systemgroup.newmonopolymarket.office.NewMonopolyMarketOfficeSeatInfo;
import com.yanqu.road.entity.log.cross.newMonopolyMarket.log.NewMonopolyMarketOfficeRobLogData;
import com.yanqu.road.logic.config.GameConfig;
import com.yanqu.road.logic.pb.PlayerBasePb;
import com.yanqu.road.pb.newmonopolymkt.NewMonopolyMarketProto;
import com.yanqu.road.pb.player.PlayerProto;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.log.CrossAutoLogMgr;
import com.yanqu.road.server.manager.user.CrossUserMgr;
import com.yanqu.road.server.manger.ConfigMgr;
import com.yanqu.road.server.manger.NewMonopolyMarketCommonMgr;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.GameProtocol;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.utils.date.DateHelper;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

public class CrossNewMonopolyMarketOffice {

    //大管家
    CrossNewMonopolyMarketGroup monopolyMarket;

    //动态生成的座位范围配置信息
    List<MonopolyMarketOfficeSeatConfig> seatConfigList;

    //榷署   ------------
    //榷署座位
    private Map<Integer, NewMonopolyMarketOfficeSeatInfo> seatDataMap;

    /**
     * 玩家采集完成的未领取奖励
     */
    public Map<Long, NewMonopolyMarketOfficeFinishData> userOfficeFinishMap = new ConcurrentHashMap<>();

    /**
     * 临时采集的奖励 入库用
     */
    public List<NewMonopolyMarketOfficeFinishData> tempOfficeFinishList = new ArrayList<>();

    /**
     *  需要update 和add 的seatInfo 改完删除
     */
    public Set<NewMonopolyMarketOfficeSeatInfo> updateAndAddSeatInfoSet = new HashSet<>();

    /**
     * 玩家 入座的位置
     */
    public Map<Long, NewMonopolyMarketOfficeSeatInfo> userSeatMap = new ConcurrentHashMap<>();

    /**
     * 玩家 当前正在查看的房间
     */
    public Map<Long,Integer> userInRoomMap = new ConcurrentHashMap<>();

    public CrossNewMonopolyMarketOffice(CrossNewMonopolyMarketGroup monopolyMarket) {
        this.monopolyMarket = monopolyMarket;
    }

    public void loadData() {

        //加载榷署座位
        seatDataMap = new NewMonopolyMarketOfficeSeatDataDaoImpl().getNewMonopolyMktOfficeSeatData(monopolyMarket.getWeekStateData().getWeekId(), monopolyMarket.getGroupId());

        for (Map.Entry<Integer, NewMonopolyMarketOfficeSeatInfo> infoEntry : seatDataMap.entrySet()) {
            NewMonopolyMarketOfficeSeatInfo seatInfo = infoEntry.getValue();
            if(seatInfo.baseInfo.getPlayerId() > 0){
                userSeatMap.put(seatInfo.baseInfo.getPlayerId(), seatInfo);
            }
        }

        //未領取的采集奖励
        userOfficeFinishMap = new NewMonopolyMarketOfficeFinishDataDaoImpl().getNewMonopolyMarketOfficeFinishData(monopolyMarket.getWeekStateData().getWeekId(), monopolyMarket.getGroupId());
    }

    public void saveData(){
        // 保存位置
        List<NewMonopolyMarketOfficeSeatInfo> tempSeatInfo;
        synchronized (updateAndAddSeatInfoSet){
            tempSeatInfo = new ArrayList<>(updateAndAddSeatInfoSet);
            updateAndAddSeatInfoSet.clear();
        }
        for (NewMonopolyMarketOfficeSeatInfo seatInfo : tempSeatInfo){
            if (seatInfo.baseInfo.isUpdateOption()) {
                new NewMonopolyMarketOfficeSeatDataDaoImpl().update(seatInfo.baseInfo);
            } else if (seatInfo.baseInfo.isInsertOption()) {
                new NewMonopolyMarketOfficeSeatDataDaoImpl().add(seatInfo.baseInfo);
            }
        }

        List<NewMonopolyMarketOfficeFinishData> list;
        synchronized (tempOfficeFinishList){
            list = new ArrayList<>(tempOfficeFinishList);
            tempOfficeFinishList.clear();
        }
        for(NewMonopolyMarketOfficeFinishData item : list){
            if (item.isUpdateOption()) {
                new NewMonopolyMarketOfficeFinishDataDaoImpl().update(item);
            } else if (item.isInsertOption()) {
                new NewMonopolyMarketOfficeFinishDataDaoImpl().add(item);
            }
        }

    }

    /**
     * 榷署每日重置
     */
    public boolean resetOneDay(){


        //大于开始时间

        return false;
    }

    /**
     * 周一22点，结算待客茶
     */
    public void sendTeaReward(int now){

        int endTime = NewMonopolyMarketCommonMgr.getResetOfficeTime(monopolyMarket.getWeekStateData().getOfficeRefreshTime(), ConfigMgr.getOfficeEndSecondNew());

        if(now >= endTime){
            synchronized (this){
                if(userOfficeFinishMap.size() == 0){
                    return;
                }
                Map<Long, List<NewMonopolyMarketOfficeFinishData>> listMap = new HashMap<>();
                List<Long> userList = new ArrayList<>(userOfficeFinishMap.keySet());
                for (long userId : userList) {
                    NewMonopolyMarketOfficeFinishData finishData = userOfficeFinishMap.remove(userId);
                    if(finishData == null || finishData.getIsSyn()) {
                        CrossNewMonopolyMarketMgr.getLogger().info("userId {} 榷署位置结算状态 ", userId);
                        continue;
                    }
                    UserBaseInfo userBaseInfo = CrossUserMgr.getUserBaseInfo(finishData.getUserId());
                    if(userBaseInfo == null){
                        continue;
                    }
                    if(!listMap.containsKey(userBaseInfo.getServerId())){
                        listMap.put(userBaseInfo.getServerId(), new ArrayList<>());
                    }
                    finishData.setIsSyn(true);
                    synchronized (tempOfficeFinishList){
                        tempOfficeFinishList.add(finishData);
                    }
                    listMap.get(userBaseInfo.getServerId()).add(finishData);
                }
                for (Map.Entry<Long, List<NewMonopolyMarketOfficeFinishData>> entry : listMap.entrySet()) {
                    long serverId = entry.getKey();
                    NewMonopolyMarketProto.NewMonopolyMarketSendOfficeSettleRewardSync.Builder sync = NewMonopolyMarketProto.NewMonopolyMarketSendOfficeSettleRewardSync.newBuilder();
                    for (NewMonopolyMarketOfficeFinishData data : entry.getValue()) {
                        NewMonopolyMarketProto.NewMonopolyMarketSendOfficeSettleTemp.Builder builder = NewMonopolyMarketProto.NewMonopolyMarketSendOfficeSettleTemp.newBuilder();
                        builder.setUserId(data.getUserId());
                        builder.setEndTime(data.getTime());
                        builder.setOfficePos(data.getPosIndex());
                        builder.setOfficeLevel(data.getLevel());
                        builder.setOfficeNo(calNo(data.getPosIndex()));
                        sync.addOfficeSettle(builder);
                    }
                    MessageHelper.sendPacket(serverId, 0, YanQuMessageUtils.buildMessage(Protocol.S_NEW_MONOPOLY_MARKET_SEND_OFFICE_END_TIME_FROM_CROSS, sync));
                }
            }
        }

    }

    public boolean refresh(int anyInWeek, int now){
        //本周的关闭时间，周日22点
        int refreshTime = NewMonopolyMarketCommonMgr.getResetOfficeTime(anyInWeek, ConfigMgr.getOfficeEndSecondNew());
        if(now >= refreshTime && monopolyMarket.getWeekStateData().getOfficeRefreshTime() < refreshTime){
            resetAllSeatInfo(refreshTime);
            monopolyMarket.getWeekStateData().setOfficeRefreshTime(refreshTime);
            CrossNewMonopolyMarketMgr.getLogger().info("new office refresh tea reward");
            return true;
        }
        return false;
    }

    /**
     * 重置所有位置
     */
    public void resetAllSeatInfo(int refreshTime){
        synchronized (this) {
            for (NewMonopolyMarketOfficeSeatInfo item : seatDataMap.values()) {
                if(item.baseInfo.getPlayerId() > 0){
                    int no = calNo(item.baseInfo.getSeatId());
                    //标识结算
                    saveFinishRecord(item.baseInfo.getPlayerId(), item.baseInfo, no, "", refreshTime, 1);
                }
                resetSeatInfo(item, true);
            }
            userSeatMap.clear();
            userInRoomMap.clear();

        }
    }

    /**
     * 进入榷署
     */
    public NewMonopolyMarketProto.NewMonopolyMarketEnterOfficeRespMsg.Builder enter(long serverId, long userId, int page, long time, boolean isGetNotice) {
        NewMonopolyMarketProto.NewMonopolyMarketEnterOfficeRespMsg.Builder builder = NewMonopolyMarketProto.NewMonopolyMarketEnterOfficeRespMsg.newBuilder();
        builder.setPage(page);
        builder.setTime(DateHelper.getCurrentSecond());
        int startIndex = page * GameConfig.NEWMONOPOLYMARKET_INITIAL_LOADNUM_1;
        int endIndex = startIndex + GameConfig.NEWMONOPOLYMARKET_INITIAL_LOADNUM_1;

        synchronized (this){
            int max = seatDataMap.size();
            for (int i = startIndex; i < endIndex; i++) {
                if(i >= max){
                    break;
                }
                NewMonopolyMarketOfficeSeatInfo seatInfo = seatDataMap.get(i);
                if(seatInfo != null){
                    if((time == 0 || time < seatInfo.baseInfo.getChangeTime())){
                        refreshSeatInfo(seatInfo.baseInfo);
                        builder.addSeatList(builderMonopolyMarketOfficeEntity(seatInfo.baseInfo,false));
                    }
                }
                if(isGetNotice){
                    NewMonopolyMarketNoticeData noticeData = monopolyMarket.getNewMonopolyMarketNoticeData(0, serverId);
                    if(noticeData != null){
                        builder.setNewNotice(monopolyMarket.builderMonopolyMarketNoticeEntity(noticeData));
                    }
                }
            }
        }
        builder.setRet(0);
        return builder;
    }

    public NewMonopolyMarketProto.NewMonopolyMarketEnterOfficeSeatRespMsg.Builder enterOfficeSeat(long serverId, long userId, int posIndex) {
        NewMonopolyMarketProto.NewMonopolyMarketEnterOfficeSeatRespMsg.Builder builder = NewMonopolyMarketProto.NewMonopolyMarketEnterOfficeSeatRespMsg.newBuilder();

        if(posIndex >= seatDataMap.size()){
            builder.setRet(GameErrorCode.E_NEW_MONOPOLY_MARKET_OFFICE_SEAT_INFO_NO_EXIST);
            return builder;
        }

        synchronized (this){
            NewMonopolyMarketOfficeSeatInfo seatInfo = seatDataMap.get(posIndex);
            refreshSeatInfo(seatInfo.baseInfo);
            seatInfo.userMap.add(userId);
            userInRoomMap.put(userId,posIndex);
            builder.setSeatInfo(builderMonopolyMarketOfficeEntity(seatInfo.baseInfo, true));
        }

        builder.setRet(0);
        return builder;
    }

    public synchronized NewMonopolyMarketProto.NewMonopolyMarketOfficeActionToGameServerMsg.Builder action(int type, int posIndex, int seatTime, long seatUserId, long userId, byte decorationClass, int decorationOverTime, UserBaseInfo userBaseInfo) {

        NewMonopolyMarketProto.NewMonopolyMarketOfficeActionToGameServerMsg.Builder builder = NewMonopolyMarketProto.NewMonopolyMarketOfficeActionToGameServerMsg.newBuilder();
        builder.setType(type);

        NewMonopolyMarketProto.NewMonopolyMarketOfficeActionRespMsg.Builder clientMsg = NewMonopolyMarketProto.NewMonopolyMarketOfficeActionRespMsg.newBuilder();
        //不是开放时间
        if(!CrossNewMonopolyMarketMgr.isSystemOpen(eMonopolyMarketResType.OFFICE_NEW.getValue())){
            clientMsg.setRet(GameErrorCode.E_NEW_MONOPOLY_MARKET_SYSTEM_NO_OPEN);
            builder.setStcMsg(clientMsg);
            return builder;
        }

        //位置不存在
        NewMonopolyMarketOfficeSeatInfo seatInfo = getSeatDataMap().get(posIndex);
        if(seatInfo.baseInfo == null){
            clientMsg.setRet(GameErrorCode.E_NEW_MONOPOLY_MARKET_OFFICE_SEAT_INFO_NO_EXIST);
            builder.setStcMsg(clientMsg);
            return builder;
        }

        //位置信息过期
        if(seatInfo.baseInfo.getSitTime() != seatTime || seatInfo.baseInfo.getPlayerId() != seatUserId) {
            clientMsg.setRet(GameErrorCode.E_NEW_MONOPOLY_MARKET_OFFICE_SEAT_INFO_CHANGE);
            builder.setStcMsg(clientMsg);
            return builder;
        }

        //等级配置不存在
        NewMonopolyMarketResConfigData configData = CrossNewMonopolyMarketConfigMgr.getMonopolyMarketResConfigData(eMonopolyMarketResType.OFFICE_NEW.getValue(), seatInfo.baseInfo.getSeatLv());
        if(configData == null){
            clientMsg.setRet(GameErrorCode.E_NEW_MONOPOLY_MARKET_OFFICE_SEAT_CONFIG_NO_EXIST);
            builder.setStcMsg(clientMsg);
            return builder;
        }

        int no = calNo(posIndex);
        int now = DateHelper.getCurrentSecond();
        boolean isChange;

        NewMonopolyMarketOfficeRobLogData robLogData = new NewMonopolyMarketOfficeRobLogData();
        robLogData.setWeekId(monopolyMarket.getWeekStateData().getWeekId());
        robLogData.setGroupId(monopolyMarket.getGroupId());
        robLogData.setPosIndex(posIndex);
        robLogData.setLevel(seatInfo.baseInfo.getSeatLv());
        robLogData.setNo(no);
        if(type == 0){
            //入座或者抢占
            if(userBaseInfo == null){
                clientMsg.setRet(GameErrorCode.E_NEW_MONOPOLY_MARKET_USER_DATA_NO_EXIST);
                builder.setStcMsg(clientMsg);
                return builder;
            }
            if(seatInfo.baseInfo.getPlayerId() == userId){
                clientMsg.setRet(GameErrorCode.E_NEW_MONOPOLY_MARKET_NO_CAN_SEATED_REPEATED_POSITION);
                builder.setStcMsg(clientMsg);
                return builder;
            }
            boolean success = false;
            if(seatInfo.baseInfo.getPlayerId() == 0){
                success = true;
                robLogData.setType(0);
            }else {
                refreshSeatInfo(seatInfo.baseInfo);
                UserBaseInfo targetUserInfo = CrossUserMgr.getUserBaseInfo(seatInfo.baseInfo.getPlayerId());

                //日志
                robLogData.setStartTime(seatInfo.baseInfo.getSitTime());
                robLogData.setUserId(seatInfo.baseInfo.getPlayerId());
                robLogData.setDecorationClass(seatInfo.baseInfo.getDecorationClass());
                robLogData.setTitle(targetUserInfo.getTitleId());
                robLogData.setPrestige(targetUserInfo.getPrestige());
                robLogData.setType(1);

                int pkRounds = 3;
                if(decorationClass != seatInfo.baseInfo.getDecorationClass()){
                    // 比较称号阶段
                    success = decorationClass > seatInfo.baseInfo.getDecorationClass();
                    pkRounds = 1;
                } else if(userBaseInfo.getTitleId() != targetUserInfo.getTitleId()){
                    // 比较身份
                    success =  userBaseInfo.getTitleId() > targetUserInfo.getTitleId();
                    pkRounds = 2;
                } else if(userBaseInfo.getPrestige() != targetUserInfo.getPrestige()){
                    // 比较声望
                    success =  userBaseInfo.getPrestige() > targetUserInfo.getPrestige();
                    pkRounds = 3;
                }
                if(success){
                    // 保存被抢记录并通知区服有奖励可领
                    saveFinishRecord(seatInfo.baseInfo.getPlayerId(), seatInfo.baseInfo, no,  userBaseInfo.getNickName(), now, 0);
                }
                // 通知被抢玩家 加战报 如果成功结算位置信息
                addOfficeReport(userId, userBaseInfo, seatInfo.baseInfo ,targetUserInfo, success, seatInfo.baseInfo.getSeatLv(), no);
                clientMsg.setPkRounds(pkRounds);
                clientMsg.setTargetInfo(PlayerBasePb.parsePlayerBaseTempMsg(targetUserInfo));
                clientMsg.setDecorationOverTime(seatInfo.baseInfo.getDecorationOverTime());
            }
            clientMsg.setIsSuccess(success);
            if(success){
                // 离开原来位置
                // 已有位置
                NewMonopolyMarketOfficeSeatInfo oldSeatData = userSeatMap.get(userId);
                if(oldSeatData != null){
                    leaveSeatInfo(oldSeatData, true);
                }
                if(configData.getIsad() == 1){
                    // 加公告
                    monopolyMarket.addNotice(eMonopolyMarketResType.OFFICE_NEW.getValue(),userId, seatInfo.baseInfo.getPlayerId(), posIndex, 0, -1, 1);
                }
                leaveSeatInfo(seatInfo, false);
                CrossUserMgr.updateUserBaseInfo(userId, userBaseInfo);
                sitDown(userId, now, posIndex, decorationClass, decorationOverTime);
                clientMsg.setSeatInfo(builderMonopolyMarketOfficeEntity(seatInfo.baseInfo,true));
            }
            isChange = success;
            builder.setSeatLevel(seatInfo.baseInfo.getSeatLv());
            builder.setNo(no);

            robLogData.setWin(success);
            robLogData.setRobId(userId);
            robLogData.setRobDecorationClass(decorationClass);
            robLogData.setRobTitle(userBaseInfo.getTitleId());
            robLogData.setRobPrestige(userBaseInfo.getPrestige());
        }else {
            //撤回
            if(seatInfo.baseInfo.getPlayerId() != userId){
                clientMsg.setRet(GameErrorCode.E_NEW_MONOPOLY_MARKET_OFFICE_IS_NOT_CURRENTLY_IN_THIS_SEAT);
                builder.setStcMsg(clientMsg);
                return builder;
            }
            UserBaseInfo targetUserInfo = CrossUserMgr.getUserBaseInfo(seatInfo.baseInfo.getPlayerId());
            robLogData.setStartTime(seatInfo.baseInfo.getSitTime());
            robLogData.setUserId(seatInfo.baseInfo.getPlayerId());
            robLogData.setDecorationClass(seatInfo.baseInfo.getDecorationClass());
            robLogData.setTitle(targetUserInfo.getTitleId());
            robLogData.setPrestige(targetUserInfo.getPrestige());
            robLogData.setType(2);

            leaveSeatInfo(seatInfo, false);
            isChange = true;
            clientMsg.setSeatInfo(builderMonopolyMarketOfficeEntity(seatInfo.baseInfo,true));
        }
        if(isChange){
            broadcastMonopolyMarketOfficeRespMsg(seatInfo, userId);
        }

        // 加日志
        robLogData.setGroupId(monopolyMarket.getWeekStateData().getSystemGroupId());
        robLogData.setWeekId(monopolyMarket.getWeekStateData().getWeekId());
        robLogData.setPosIndex(posIndex);
        robLogData.setLevel(seatInfo.baseInfo.getSeatLv());
        robLogData.setNo(no);
//        robLogData.setLogTime(new Date());
//        NewMonopolyMarketLogMgr.addMonopolyMarketOfficeRobLogData(robLogData);
        CrossAutoLogMgr.add(robLogData);

        clientMsg.setRet(0);
        builder.setStcMsg(clientMsg);
        return builder;
    }

    /**
     * 离开位置
     * @param seatInfo
     */
    private void leaveSeatInfo(NewMonopolyMarketOfficeSeatInfo seatInfo, boolean isBroadcast){
        userSeatMap.remove(seatInfo.baseInfo.getPlayerId());
        resetSeatInfo(seatInfo, false);
        seatInfo.baseInfo.setChangeTime(DateHelper.getCurrentSecond());
        synchronized (updateAndAddSeatInfoSet){
            updateAndAddSeatInfoSet.add(seatInfo);
        }
        if(isBroadcast){
            broadcastMonopolyMarketOfficeRespMsg(seatInfo, 0);
        }
    }

    /**
     * 清空位置信息
     */
    private void resetSeatInfo(NewMonopolyMarketOfficeSeatInfo seatInfo, boolean isSave){
        seatInfo.baseInfo.setPlayerId(0);
        seatInfo.baseInfo.setSitTime(0);
        seatInfo.baseInfo.setDecorationClass((byte)-1);
        seatInfo.baseInfo.setDecorationOverTime(-1);
        seatInfo.baseInfo.setChangeTime(0);
        if(isSave){
            synchronized (updateAndAddSeatInfoSet){
                updateAndAddSeatInfoSet.add(seatInfo);
            }
        }
    }

    /**
     * 入座
     * @param userId
     * @param seatTime s
     * @param index
     * @return
     */
    public void sitDown(long userId, int seatTime, int index, byte decorationClass, int decorationOverTime){
        NewMonopolyMarketOfficeSeatInfo seatInfo = getSeatDataMap().get(index);
        seatInfo.baseInfo.setPlayerId(userId);
        seatInfo.baseInfo.setSitTime(seatTime);
        seatInfo.baseInfo.setDecorationClass(decorationClass);
        seatInfo.baseInfo.setDecorationOverTime(decorationOverTime);

        synchronized (updateAndAddSeatInfoSet){
            updateAndAddSeatInfoSet.add(seatInfo);
        }
        userSeatMap.put(userId, seatInfo);
    }

    /**
     * 给房间里面的玩家广播座位信息
     * @param seatInfo
     * @return
     */
    private void broadcastMonopolyMarketOfficeRespMsg(NewMonopolyMarketOfficeSeatInfo seatInfo, long excludeId){
        NewMonopolyMarketProto.NewBroadcastMonopolyMarketRespMsg.Builder builder = NewMonopolyMarketProto.NewBroadcastMonopolyMarketRespMsg.newBuilder();
        builder.setType(0);
        builder.setSeatInfo(builderMonopolyMarketOfficeEntity(seatInfo.baseInfo,true));
        for(long userId : seatInfo.userMap){
            if(userId != excludeId){
                MessageHelper.sendPacket(CrossUserMgr.getServerIdByUserId(userId), userId, YanQuMessageUtils.buildMessage(Protocol.U_NEW_MONOPOLY_MARKET_BROADCAST, builder));
            }
        }
    }

    /**
     * 退出大宋榷署位置
     * @param userId
     * @return
     */
    public void exitOfficeSeat(int index, long userId) {
        NewMonopolyMarketOfficeSeatInfo seatInfo = seatDataMap.get(index);
        if(seatInfo != null){
            synchronized (this) {
                seatInfo.userMap.remove(userId);
                userInRoomMap.remove(userId);
            }
        }
    }

    private void addOfficeReport(long attackerId, UserBaseInfo attackerInfo, NewMonopolyMarketOfficeSeatData seatData, UserBaseInfo defenderInfo, boolean isWin, int level, int no){
        NewMonopolyMarketProto.NewMonopolyMarketSynAttackToGameServerMsg.Builder builder = NewMonopolyMarketProto.NewMonopolyMarketSynAttackToGameServerMsg.newBuilder();
        builder.setType(eMonopolyMarketResType.OFFICE_NEW.getValue());
        builder.setRobedId(seatData.getPlayerId());
        builder.setPosIndex(seatData.getSeatId());
        builder.setAttackId(attackerId);
        if(attackerInfo.getServerId() != defenderInfo.getServerId()){
            builder.setAttackInfo(PlayerBasePb.parsePlayerBaseTempMsg(attackerInfo));
        }
        builder.setIsWin(isWin);
        builder.setLevel(level);
        builder.setNo(no);
        builder.setResGroupId(0);
        builder.setWeekId(monopolyMarket.getWeekStateData().getWeekId());
        builder.setSystemGroupId(monopolyMarket.getWeekStateData().getSystemGroupId());
        MessageHelper.sendPacket(defenderInfo.getServerId(), seatData.getPlayerId(), YanQuMessageUtils.buildMessage(GameProtocol.S_NEW_MONOPOLY_MARKET_SYN_ATTACK, builder));
    }

    private void saveFinishRecord(long userId, NewMonopolyMarketOfficeSeatData seatData, int no, String robName, int now, int finishType){
        NewMonopolyMarketOfficeFinishData officeFinishData = userOfficeFinishMap.get(userId);
        if(officeFinishData == null){
            officeFinishData = new NewMonopolyMarketOfficeFinishData();
            officeFinishData.setWeekId(monopolyMarket.getWeekStateData().getWeekId());
            officeFinishData.setGroupId(monopolyMarket.getGroupId());
            officeFinishData.setInsertOption();
            officeFinishData.setId(CrossNewMonopolyMarketMgr.maxOfficeFinishId.incrementAndGet());
            officeFinishData.setUserId(userId);
            officeFinishData.setPosIndex(seatData.getSeatId());
            officeFinishData.setLevel((byte) seatData.getSeatLv());
            officeFinishData.setNo(no);
            officeFinishData.setFinishType(finishType);
            officeFinishData.setRobName(robName);
            officeFinishData.setTime(now);
            userOfficeFinishMap.put(userId,officeFinishData);
        }
        synchronized (tempOfficeFinishList){
            tempOfficeFinishList.add(officeFinishData);
        }
    }

    /**
     * 刷新玩家称号
     */
    private static void refreshSeatInfo(NewMonopolyMarketOfficeSeatData seatData){
        if(seatData.getDecorationOverTime() != -1 && seatData.getDecorationOverTime() < DateHelper.getCurrentSecond()){
            seatData.setDecorationClass((byte)-1);
            seatData.setDecorationOverTime(-1);
        }
    }

    /**
     * 计算位置编号 从1开始
     * @return
     */
    public int calNo(int posIndex){
        int no = 0;
        for(MonopolyMarketOfficeSeatConfig item : seatConfigList){
            if(posIndex >= item.startIndex && posIndex <= item.endIndex){
                no = posIndex - item.startIndex  + 1;
            }
        }
        return no;
    }

    public List<MonopolyMarketOfficeSeatConfig> getSeatConfigList() {
        return seatConfigList;
    }

    public void setSeatConfigList(List<MonopolyMarketOfficeSeatConfig> seatConfigList) {
        this.seatConfigList = seatConfigList;
    }

    //--
    private static NewMonopolyMarketProto.NewMonopolyMarketOfficeEntity.Builder builderMonopolyMarketOfficeEntity(NewMonopolyMarketOfficeSeatData seatData, boolean showAllPlayer){
        NewMonopolyMarketProto.NewMonopolyMarketOfficeEntity.Builder builder = NewMonopolyMarketProto.NewMonopolyMarketOfficeEntity.newBuilder();
        builder.setPosIndex(seatData.getSeatId());
        builder.setUserId(seatData.getPlayerId());
        if(seatData.getPlayerId() > 0){
            UserBaseInfo seatUserBaseInfo = CrossUserMgr.getUserBaseInfo(seatData.getPlayerId());
            if(showAllPlayer){
                builder.setUserInfo(PlayerBasePb.parsePlayerBaseTempMsg(seatUserBaseInfo));
            }else {
                builder.setUserInfo(parseSimplePlayerBaseTempMsg(seatUserBaseInfo));
            }
            builder.setDecorationOverTime(seatData.getDecorationOverTime());
            builder.setSeatTime(seatData.getSitTime());
        }
        return builder;
    }

    private static PlayerProto.PlayerBaseTempMsg.Builder parseSimplePlayerBaseTempMsg(UserBaseInfo userBaseInfo){
        PlayerProto.PlayerBaseTempMsg.Builder msg = PlayerProto.PlayerBaseTempMsg.newBuilder();
        msg.setNickName(userBaseInfo.getNickName());
        msg.setTitleId(userBaseInfo.getTitleId());
        msg.setDecoration(userBaseInfo.getDecoration());
        msg.setSex(userBaseInfo.getSex());
        msg.setServerId(userBaseInfo.getServerId());
        msg.setWorld(userBaseInfo.getWorld());
        msg.setContinent(userBaseInfo.getContinent());
        msg.setCountry(userBaseInfo.getCountry());
        msg.setProvince(userBaseInfo.getProvince());
        msg.setCity(userBaseInfo.getCity());
        msg.setHideVipAndAddress(userBaseInfo.getHideVipAndAddress());
        return msg;
    }


    //set

    public void setSeatDataMap(Map<Integer, NewMonopolyMarketOfficeSeatInfo> seatDataMap) {
        this.seatDataMap = seatDataMap;
    }

    public Map<Integer, NewMonopolyMarketOfficeSeatInfo> getSeatDataMap() {
        return seatDataMap;
    }

}
